/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月20日         luwei
 */

package com.coocaa.x.uipackage;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnHoverListener;
import android.view.View.OnKeyListener;
import android.view.animation.Animation;
import android.widget.FrameLayout;

import com.coocaa.x.framework.app.CoocaaActivity;
import com.coocaa.x.uipackage.SkinManager.OnThemeChangeListener;

public class BaseView extends FrameLayout implements OnThemeChangeListener, OnHoverListener,
        OnGestureListener, OnKeyListener, OnClickListener, OnFocusChangeListener {
    private static final Handler uiHandler = new Handler(Looper.getMainLooper());

    // private GestureDetector mGestureDetector = null;
    protected int mIndex = 0;
    protected Context mContext = null;
    private int mShadowX = 0;
    private int mShadowY = 0;

    private Animation inAnimation = null, outAnimation = null;

    public BaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public BaseView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    private void init() {
        enableFocusable(true);
        // setOnHoverListener(this);
        // setOnTouchListener(this);
        setOnClickListener(this);
        setOnKeyListener(this);
        // setOnFocusChangeListener(this);
        // mGestureDetector = new GestureDetector(this);
    }

    public void enableFocusable(boolean v) {
        setFocusable(v);
        setFocusableInTouchMode(v);
    }

    public void setIndex(int index) {
        this.mIndex = index;
    }

    private OnThemeChangeListener themeListener;

    public void setOnThemeChangeListener(OnThemeChangeListener listener) {
        this.themeListener = listener;
    }

    @Override
    public void onThemeChange() {
        if (themeListener != null)
            themeListener.onThemeChange();
    }

    public void setShadowSize(int x, int y) {
        this.mShadowX = x;
        this.mShadowY = y;
    }

    // @Override
    // public boolean onTouch(View v, MotionEvent event)
    // {
    // return mGestureDetector.onTouchEvent(event);
    // }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onHover(View v, MotionEvent event) {
        int what = event.getAction();
        switch (what) {
            case MotionEvent.ACTION_HOVER_ENTER: // 鼠标进入view
                break;
            case MotionEvent.ACTION_HOVER_MOVE: // 鼠标在view上
                if (event.getX() > mShadowX && event.getY() > mShadowY) {
                    requestFocus();
                }
                break;
            case MotionEvent.ACTION_HOVER_EXIT: // 鼠标离开view
                break;
        }
        return false;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                if (shieldLeftKey()) {
                    return true;
                }
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (shieldRightKey()) {
                    return true;
                }
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                if (shieldUpKey()) {
                    return true;
                }
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                if (shieldDownKey()) {
                    return true;
                }
                break;
            default:
                break;
        }
        return false;
    }

    public void show() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (inAnimation == null)
                    inAnimation = getShownAnimation();
                if (inAnimation != null)
                    startAnimation(inAnimation);
                setVisibility(View.VISIBLE);
            }
        });
    }

    public void hide() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (outAnimation == null)
                    outAnimation = getHidenAnimation();
                if (outAnimation != null)
                    startAnimation(outAnimation);
                setVisibility(View.GONE);
            }
        });
    }

    protected Animation getShownAnimation() {
        return null;
    }

    protected Animation getHidenAnimation() {
        return null;
    }

    protected boolean shieldLeftKey() {
        return false;
    }

    protected boolean shieldRightKey() {
        return false;
    }

    protected boolean shieldUpKey() {
        return false;
    }

    protected boolean shieldDownKey() {
        return false;
    }

    protected static final void runOnUiThread(Runnable run) {
        uiHandler.post(run);
    }

    protected static final void runOnUiThread(Runnable run, long delay) {
        uiHandler.postDelayed(run, delay);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    public static int Div(int x) {
        return CoocaaActivity.Div(x);
    }

    public static int Dpi(int x) {
        return CoocaaActivity.Dpi(x);
    }
}
